package com.hlkj.warboot.configurer.util.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.hlkj.warboot.configurer.util.excel.admin.WriteAdminEntity;

import java.io.File;
import java.util.*;
import java.util.stream.Stream;

/**
 * @Description:  TODO 基于EasyExcel的Excel工具类
 * @Author: lixing
 * @Date: 2020/12/5 9:26
 */
public class EasyExcelUtil {
    public static void main(String[] args) {
        // 导出模板
        File templateFileName = new File("C:\\Users\\lixing\\Desktop\\admin导出模板.xlsx");
        // 导出文件地址
        String fileName = "C:\\Users\\lixing\\Desktop\\"+System.currentTimeMillis()+".xlsx";
        //
        // 表头数据
        Map<String, Object> map = new HashMap<>();
        map.put("time", "2019年10月9日13:28:28");
        // 列表数据
        WriteAdminEntity writeAdminEntity = new WriteAdminEntity();
        writeAdminEntity.setCreateTime(new Date());
        writeAdminEntity.setAccount("张三");
        writeAdminEntity.setAge(29);
        WriteAdminEntity writeAdminEntity2 = new WriteAdminEntity();
        writeAdminEntity2.setCreateTime(new Date());
        writeAdminEntity2.setAccount("李四");
        writeAdminEntity2.setAge(30);
        WriteAdminEntity writeAdminEntity3 = new WriteAdminEntity();
        writeAdminEntity3.setCreateTime(new Date());
        writeAdminEntity3.setAccount("王五");
        writeAdminEntity3.setAge(31);
        WriteAdminEntity writeAdminEntity4 = new WriteAdminEntity();
        writeAdminEntity4.setCreateTime(new Date());
        writeAdminEntity4.setAccount("赵六");
        writeAdminEntity4.setAge(32);
        WriteAdminEntity writeAdminEntity5 = new WriteAdminEntity();
        writeAdminEntity5.setCreateTime(new Date());
        writeAdminEntity5.setAccount("孙琪");
        writeAdminEntity5.setAge(33);
        //
        List<WriteAdminEntity> writeAdminEntityArrayList = new ArrayList<>();
        writeAdminEntityArrayList.add(writeAdminEntity);
        writeAdminEntityArrayList.add(writeAdminEntity2);
        writeAdminEntityArrayList.add(writeAdminEntity3);
        writeAdminEntityArrayList.add(writeAdminEntity4);
        writeAdminEntityArrayList.add(writeAdminEntity5);
        //
        // 待导出的数据
        int dataSize = writeAdminEntityArrayList.size();
        // 每个sheet存储数据的大小
        int pageSize = 2;
        // 需要的sheet数
        int sheetPageCount = dataSize > pageSize ? (dataSize/pageSize) + (dataSize%pageSize) : 1;
        // 导出
        ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
        Stream.iterate(1, index -> index + 1).limit(sheetPageCount).forEach(index -> {
            List<WriteAdminEntity> temp = null;
            if (index != sheetPageCount) {
                temp = writeAdminEntityArrayList.subList(0, pageSize);
            } else {
                temp = writeAdminEntityArrayList.subList(0, dataSize - (pageSize * (index - 1) ) );
            }
            if (index==1) {
                WriteSheet writeSheet = EasyExcel.writerSheet(index-1).build();
                excelWriter.fill(map, writeSheet);
                excelWriter.fill(temp, writeSheet);
            } else {
                WriteSheet writeSheet = EasyExcel.writerSheet(index-1,"sheet"+index).build();
                excelWriter.write(temp, writeSheet);
            }
            writeAdminEntityArrayList.removeAll(temp);
        });
        excelWriter.finish();
    }
}
